<template>
    <div class="wrap">
        <!--信息头-->
        <span>发布需求</span>
        <el-divider></el-divider>
        <div class="banner">
            <el-card class="card_class">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-tabs v-model="activeIndex" :tab-position="'left'" :before-leave="beforeLeave">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="需求标题" prop="reqName">
                            <el-input v-model="ruleForm.reqName"></el-input>
                        </el-form-item>
                        <el-form-item label="需求商品" prop="reqTypeId">
                            <el-select v-model="ruleForm.reqTypeId" placeholder="请选择商品大类" @change="onTypeChange">
                                <el-option label="铝土矿" value="1"></el-option>
                                <el-option label="氧化铝" value="2"></el-option>
                                <el-option label="电解铝" value="3"></el-option>
                                <el-option label="成品铝" value="4"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="需求详情" prop="reqDesc">
                            <el-input v-model="ruleForm.reqDesc"></el-input>
                        </el-form-item>
                        <el-form-item label="需求商名" prop="firmName">
                            <el-input v-model="ruleForm.firmName"></el-input>
                        </el-form-item>
                        <el-form-item label="预期价格" prop="reqPrice">
                            <el-input v-model="ruleForm.reqPrice" type="number"   @keydown.native="inputLimit"></el-input>
                        </el-form-item>
                        <el-form-item label="过期时间" prop="reqExpiration">
                            <!-- <el-input v-model="ruleForm.reqExpiration"></el-input> -->
                            <el-date-picker
                                v-model="ruleForm.reqExpiration"
                                type="date"
                                placeholder="选择过期日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-tab-pane>
                    <!--<el-tab-pane label="商品参数" name="1">
                         需要读取分类数据(spu-id) 
                        <div v-if="this.ruleForm.req_spu_id!==''">
                            <el-form-item v-for="(spec,index) in goods_spec" :key="index" :label="spec">
                                <el-input v-model="ruleForm.cols[index]"></el-input>
                            </el-form-item>
                        </div>
                        <el-form-item label="参数" v-if="this.ruleForm.req_spu_id===''" >
                            <el-input placeholder="无可用参数" :disabled="true"></el-input>
                        </el-form-item>
                    </el-tab-pane>-->
                    <el-tab-pane label="联系方式" name="1">
                            <el-form-item label="联系电话" prop="firmPhone">
                                <el-input v-model="ruleForm.firmPhone" type="number"  @keydown.native="inputLimit"></el-input>
                            </el-form-item>
                            <el-form-item label="联系邮箱" prop="firmEmail">
                                <el-input v-model="ruleForm.firmEmail"></el-input>
                            </el-form-item>

                            <el-form-item align="right">
                                <el-button type="primary" @click="submitForm">发布</el-button>
                                <el-button>取消</el-button>
                            </el-form-item>
                        </el-tab-pane>
                    </el-tabs>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script>
    import axios from "@/utils/request"
    export default {
        data() {
            return {
                imageUrl: '',
                ruleForm: {
                    reqName: '',
                    reqTypeId: '',
                    reqDesc: '',
                    firmName: '',
                    firmPhone: '',
                    firmEmail: '',
                    reqExpiration: '',
                    reqSpec: '',//汇总用
                    cols:["1","2","3"],
                    reqPrice:'',
                },
                rules: {
                    reqName: [
                        { required: true, message: '请输入需求名称', trigger: 'blur' },
                        { max: 50, message: '长度50个字符以内', trigger: 'blur' }
                    ],
                    reqTypeId: [
                        { required: true, message: '请选择需求商品大类', trigger: 'change' }
                    ],
                    firmEmail:[
                            {
                                required: false,
                                pattern:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
                                message: '请输入正确的邮箱',
                                trigger: 'blur'
                        }
                    ],
                    reqExpiration: [
                        { type: 'date', required: true, message: '请选择需求过期日期', trigger: 'change' }  
                        // 和现在日期的对比，是否需要设置规则
                    ],
                    reqDesc: [
                        { required: true, message: '请填写需求详情', trigger: 'blur' }
                    ]
                },
                selectedId:"",
                selectedSpu:"",
                spuList:[],
                activeIndex:"0",
                goodsSpecList: {"1":["含铝量","铝硅比"],
                                "2":["含铝量","铝硅比"],
                                "3":["长","宽","高"],
                                "4":["长","宽","高"],
                                "5":["长","宽","高"],
                                "6":["长","宽","高"],
                                "7":["平均粒度"],
                                "8":["长","宽","高"],
                                "9":["长","宽","高"],
                },
                goods_spec:[],//属性名
                testList:{"1":[{"goods_spu_id":1,"goods_spu_name":"古风化壳矿土",},{"goods_spu_id":2,"goods_spu_name":"红土"}],
                      "2":[{"goods_spu_id":7,"goods_spu_name":"α型氧化铝",}],
                      "3":[{"goods_spu_id":4,"goods_spu_name":"AC4D",},{"goods_spu_id":5,"goods_spu_name":"AC5A"},{"goods_spu_id":6,"goods_spu_name":"A308"}],
                        "4":[{"goods_spu_id":8,"goods_spu_name":"铝条",},{"goods_spu_id":9,"goods_spu_name":"铝板",}]
                },
                getUrl:"http://localhost:8009/lv-industry-app/goodsSpu/get-spu/",
                getTypeUrl:"http://localhost:8009/lv-industry-app/goodsType/",
                postReqUrl:"http://localhost:8009/lv-industry-app/requirement/create",
            };
        },
        // todo: 1.
        methods: {
            onTypeChange()
            {
                // this.spuList = this.getSpuList()
                this.spuList = this.testList[this.ruleForm.reqTypeId]
            },
            onSpuChange()
            {
                this.goods_spec = this.goodsSpecList[this.ruleForm.req_spu_id]
            },
            inputLimit(e)
            {
                let key = e.key
                if(key === 'e' || key === '.')
                {
                    this.$message.error("只能输入数字！")
                    e.returnValue = false
                    return false
                }
                return true
            },
            beforeLeave(activeName,oldActiveName)
            {
                if(oldActiveName === '0' && this.ruleForm.reqTypeId === '')
                {
                    this.$message.error("请先选择商品大类")
                    return false
                }

                return true
            },
            async getSpuList()
            {
                await axios.get(this.getUrl+this.ruleForm.goods_type_id).then((result)=>{
                    this.spuList = result.data  //列表
                })
            },
            async getSpecName()
            {
                
                this.goods_spec = []
                this.ruleForm.cols = []
                await axios.get(this.getUrl2 + this.ruleForm.goods_spu_id).then((result)=>{
                    //如果这里仍是字符串则需要转为json
                    //否则如果是json格式直接将key都放[](goods_spec)里面去
                    for(var i in result.data)
                    {
                        this.goods_spec.push(i)
                        this.cols.push(i)
                    }
                })
            },
            submitForm() {
                // this.ruleForm.reqSpec = JSON.stringify(this.ruleForm.cols)
                let formData = JSON.stringify(this.ruleForm)
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        alert('submit!');
                        console.log(formData)
                        axios.post(this.postReqUrl,formData,{headers:{
            'Content-Type': 'application/json'
        }}).then((result)=>{
                            console.log(result)
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm() {
                this.$refs.ruleForm.resetFields();
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style lang="less" scoped>
    .wrap{
        .banner{
            width: 1200px;
            height: 100%;
            .card_class{
                width: 800px;
                height: 100%;
                margin: 0 auto;
            }
        }
    }
    // 上传图片样式
    .avatar-uploader{
        width: 178px;
        height: 178px;
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
